package com.hb.game.pk.ui.activity;

import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hb.game.pk.APP;
import com.hb.game.pk.R;
import com.hb.game.pk.api.Host;
import com.hb.game.pk.api.RedBag;
import com.hb.game.pk.base.BaseActivity;
import com.hb.game.pk.beans.FriendBean;
import com.hb.game.pk.beans.OneData;
import com.hb.game.pk.network.RetrofitCreateHelper;
import com.hb.game.pk.ui.activity.contact.TransActivity;
import com.hb.game.pk.utils.StringUtils;
import com.hb.game.pk.utils.ToastUtils;
import com.google.gson.Gson;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class SearchActivity extends BaseActivity<OneData> {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.et_content)
    EditText etContent;
    @BindView(R.id.iv_head)
    ImageView ivHead;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.ll_result)
    LinearLayout llResult;
    @BindView(R.id.tv_ok)
    TextView tvOk;
    private FriendBean.DataBean data;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_search;
    }

    @Override
    protected int getRootViewId() {
        return R.id.layout_root;
    }

    @Override
    protected int getRefreshId() {
        return 0;
    }

    @Override
    protected int getListViewId() {
        return 0;
    }


    public void requestNet(String s) {
        call = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).search(s);
        call.enqueue(new Callback<OneData>() {
            @Override
            public void onResponse(Call<OneData> call, Response<OneData> response) {
                OneData body = response.body();
                if (body == null) {
                    ToastUtils.showToast("请求错误");
                    return;
                }

                if (body.getStatus() != 1) {
                    ToastUtils.showToast(body.getInfo());
                    return;
                }
                data = body.getData();
                String trim = StringUtils.getString(data.getFace());
                int res = 0;
                if (StringUtils.isEmpty(trim)) {
                    res = R.mipmap.ic_head_normal;
                } else {
                    int i = Integer.parseInt(trim);
                    res = APP.headResource.get(i - 1);
                }
                ivHead.setBackgroundResource(res);
                tvName.setText(data.getNickname());
                llResult.setVisibility(View.VISIBLE);
            }

            @Override
            public void onFailure(Call<OneData> call, Throwable t) {
                ToastUtils.showToast("网络请求错误");
            }
        });
    }

    @OnClick({R.id.iv_back, R.id.tv_ok, R.id.ll_result})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                this.finish();
                break;
            case R.id.tv_ok:
                String s = etContent.getText().toString();
                if (StringUtils.isEmpty(s)) {
                    ToastUtils.showToast("请输入手机号");
                    return;
                }
                requestNet(s);
                break;
            case R.id.ll_result:
                Intent intent = new Intent();
                intent.putExtra("friend", new Gson().toJson(data));
                startActivity(TransActivity.class, intent);
                break;
        }
    }
}
